package event

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/prometheus/common/log"
	cloudevents "github.com/cloudevents/sdk-go/v2"
)

func Fire(host,uri ,eds string ){
	c, err := cloudevents.NewClientHTTP()
	if err != nil {
		log.Fatalf("failed to create client, %v", err)
	}

	// Create an Event.
	event :=  cloudevents.NewEvent()
	event.SetSource("example/uri")
	event.SetType("example.type")
	event.SetSubject("A")
	event.SetData(cloudevents.ApplicationJSON, map[string]string{"host":host,"uri":uri})

	event.SetDataContentType(cloudevents.ApplicationJSON)
	ctx := cloudevents.ContextWithTarget(context.Background(), eds)
	bytes, err := json.Marshal(event)
	fmt.Println(string(bytes))
	// Send that Event.
	result := c.Send(ctx, event);
	if cloudevents.IsUndelivered(result) {
		log.Fatalf("failed to send, %v", result)
	}else{
		log.Info("收到faas响应:",result)
	}
}
